
cd('.')
addpath('matlab functions')

OUT = struct();

for sbj = {'math','reading'}
    for policy_sim = 1:2

        outname = ([sbj{1} num2str(policy_sim)]);
        load(sprintf('output/reallocation/%s_%d_matching.mat',sbj{1},policy_sim));
        TBL.quant = 1 + (TBL.(sprintf('lag_%sscore',sbj{1}))>=norminv(1/3)) + (TBL.(sprintf('lag_%sscore',sbj{1}))>=norminv(2/3));
        OUT.(outname).race_gender = groupsummary(TBL,{'female','race_black'},'mean','VA_gain');
        OUT.(outname).lagscore = groupsummary(TBL,{'quant'},'mean','VA_gain');
        OUT.(outname).overall = groupsummary(TBL,[],'mean',{'VA_gain','VA_gain_b'});

        for f = {'race_gender','lagscore','overall'}
            OUT.(outname).(f{1}) = removevars(OUT.(outname).(f{1}),'GroupCount');
            OUT.(outname).(f{1}) = renamevars(OUT.(outname).(f{1}),'mean_VA_gain',['VA_gain' num2str(policy_sim)]);
        end        
    end
end

% race by gender
race_gender = innerjoin(OUT.math2.race_gender,OUT.reading2.race_gender,'Keys',{'female','race_black'});
race_gender = innerjoin(race_gender,OUT.math1.race_gender,'Keys',{'female','race_black'});
race_gender = innerjoin(race_gender,OUT.reading1.race_gender,'Keys',{'female','race_black'});
race_gender = [cell2table({'Non-Black Males','Black Males','Non-Black Females','Black Females'}','VariableNames',{'name'}) ...
    race_gender(:,3:end)];
latextbl(race_gender,... 
    'tables and figures/tables/reallocation_race_gender.tex', ...
    {'%s','%0.4f','%0.4f','%0.4f','%0.4f'});

%lagscore
lagscore = innerjoin(OUT.math2.lagscore,OUT.reading2.lagscore,'Keys',{'quant'});
lagscore = innerjoin(lagscore,OUT.math1.lagscore,'Keys',{'quant'});
lagscore = innerjoin(lagscore,OUT.reading1.lagscore,'Keys',{'quant'});
lagscore = [cell2table({'Bottom One-Third','Middle One-Third','Top One-Third'}','VariableNames',{'name'}) lagscore(:,2:end)];
latextbl(lagscore,... 
    'tables and figures/tables/reallocation_lagscore.tex', ...
    {'%s','%0.4f','%0.4f','%0.4f','%0.4f'});

%overall
overall = [OUT.math2.overall.VA_gain2 OUT.reading2.overall.VA_gain2 ...
    OUT.math1.overall.VA_gain1 OUT.reading1.overall.VA_gain1];
overall = [{'Overall'} num2cell(overall)];
latextbl(overall,... 
    'tables and figures/tables/reallocation_overall.tex', ...
    {'%s','%0.4f','%0.4f','%0.4f','%0.4f'});



